package com.shengsiyuan.netty.fifthexample;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * Created by deng_wb on 2019/8/3
 */
public class WebSocketChannelInitializer  extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new HttpServerCodec()); // HTTP服务器编解码器
        pipeline.addLast(new ChunkedWriteHandler()); // 分块写入处理程序
        pipeline.addLast(new HttpObjectAggregator(8192));// HTTP对象聚合器
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));// WebSocket服务器协议处理程序
        pipeline.addLast(new TextWebSocketFrameHandler());

    }
}
